using System;
using System.Diagnostics;
using System.Text;
using System.Web;
using System.Windows.Forms;
using Client.Properties;

namespace Client.UIHelper
{
    /// <summary>
    ///  The PayPal class is responsible for generating the URL attributes required to open a web
    ///  browser window for PayPal payments.
    /// </summary>
    public class Paypal
    {
        private readonly string _accountEmail;
        private readonly string _amount;
        private readonly string _cancelUrl;
        private readonly string _currencyCode;
        private readonly string _devID;
        private readonly bool _includeNote;
        private readonly string _invoice;
        private readonly string _itemName;
        private readonly string _itemNumber;
        private readonly string _logoUrl;
        private readonly string _noteTitle;
        private readonly string _notifyUrl;
        private readonly string _quantity;
        private readonly bool _requireShippingAddress;
        private readonly string _successUrl;
        private readonly DataStore dataStore;
        private string _tag;


        /// <summary>
        /// The constructor for the paypal URL generation class.
        /// </summary>
        /// <param name="tguid">The uniqueIdentifier to pass to payPal so we can
        /// associate a transaction with a common known identity in the server database.</param>
        public Paypal(string tguid, string amount, Form formRef)
        {
            dataStore = new DataStore(formRef);

            _accountEmail = dataStore.PaymentSettings.PayPalAccountEmail;
            _logoUrl = dataStore.PaymentSettings.PayPalLogoURL;
            _itemNumber = dataStore.PaymentSettings.PayPalItemNumber;
            _quantity = dataStore.PaymentSettings.PayPalQuantity.ToString();
            _currencyCode = dataStore.PaymentSettings.PayPalCurrencyCode;
            _tag = dataStore.PaymentSettings.PayPalTag;
            _noteTitle = dataStore.PaymentSettings.PayPalNoteTitle;
            _notifyUrl = dataStore.PaymentSettings.PayPalNotifyURL;
            _successUrl = dataStore.PaymentSettings.PayPalSuccessURL;
            _cancelUrl = dataStore.PaymentSettings.PayPalCancelURL;
            _requireShippingAddress = dataStore.PaymentSettings.PayPalRequireShippingAddress;
            _includeNote = dataStore.PaymentSettings.PayPalIncludeNote;
            _devID = Settings.Default.DevToken;
            _itemName = Settings.Default.ProductName;
            _amount = amount;

            _invoice = tguid;
        }

        /// <summary>
        /// This creates a new IE window, generates the paypal URL, and passes
        /// the URL to the window via the proc.StartInfo.Arguments.
        /// </summary>
        public void Handlepayment()
        {
            //launch the IE window with the Paypal URL
            var proc = new Process();

            proc.StartInfo.FileName = "IExplore.exe";
            proc.StartInfo.Arguments = generatePayPalURL();
            proc.Start();
            proc.Dispose();
        }

        #region generate the URL based on the private parameters

        private string generatePayPalURL()
        {
            var url = new StringBuilder();

            url.AppendFormat(dataStore.PaymentSettings.PaymentURL + "?cmd={0}&business={1}", "_xclick", HttpUtility.UrlEncode(_accountEmail));

            if (!String.IsNullOrEmpty(_logoUrl))
                url.AppendFormat("&image_url={0}", HttpUtility.UrlEncode(_logoUrl));

            if (!String.IsNullOrEmpty(_itemNumber))
                url.AppendFormat("&item_number={0}", HttpUtility.UrlEncode(_itemNumber));

            if (!String.IsNullOrEmpty(_itemName))
                url.AppendFormat("&item_name={0}", HttpUtility.UrlEncode(_itemName));

            if (!String.IsNullOrEmpty(_quantity))
                url.AppendFormat("&quantity={0}", HttpUtility.UrlEncode(_quantity));

            if (!String.IsNullOrEmpty(_amount))
                url.AppendFormat("&amount={0}", HttpUtility.UrlEncode(_amount));

            if (!String.IsNullOrEmpty(_currencyCode))
                url.AppendFormat("&currency_code={0}", HttpUtility.UrlEncode(_currencyCode));

            if (!_requireShippingAddress)
                url.Append("&no_shipping=1");

            if (!_includeNote)
                url.Append("&no_note=1");

            if (!String.IsNullOrEmpty(_devID))
                url.AppendFormat("&custom={0}", HttpUtility.UrlEncode(_devID));

            if (!String.IsNullOrEmpty(_invoice))
                url.AppendFormat("&invoice={0}", HttpUtility.UrlEncode(_invoice));

            if (!String.IsNullOrEmpty(_notifyUrl) && _includeNote)
                url.AppendFormat("&cn={0}", HttpUtility.UrlEncode(_noteTitle));

            //not used in this implementation
            //if (BackgroundColor != BackgroundColor.Default)
            //	url.AppendFormat("&cs={0}", BackgroundColor == BackgroundColor.White ? "0" : "1");

            if (!String.IsNullOrEmpty(_notifyUrl))
                url.AppendFormat("&notify_url={0}", HttpUtility.UrlEncode(_notifyUrl));

            if (!String.IsNullOrEmpty(_successUrl))
                url.AppendFormat("&return={0}", HttpUtility.UrlEncode(_successUrl));

            if (!String.IsNullOrEmpty(_cancelUrl))
                url.AppendFormat("&cancel_return={0}", HttpUtility.UrlEncode(_cancelUrl));

            return url.ToString();
        }

        #endregion
    }
}